/**
 * @Title: settleAnalyse_service.js
 * @Description: 给结算指标分析器提供服务
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年9月26日 下午2:57:20
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var debug = false,
	
		debugUrls = {
			getIndicatorInfo:'mock/dataAnalyse/settlement/getIndicatorInfo.json',
			getRegionOrgs:'indication/getRegionOrgs.do',//'mock/dataAnalyse/settlement/getRegionOrgs.json',		//获取地区机构列表
			getDepartments:'indication/getDepartments.do',//'mock/dataAnalyse/settlement/getDepartments.json',	//获取科室列表
			getDoctors:'indication/getDoctors.do',//'mock/dataAnalyse/settlement/getDoctors.json',			//获取医生列表
			getOrgInfo:'mock/dataAnalyse/settlement/getOrgInfo.json',			//获取医院信息
			getDepartInfo:'mock/dataAnalyse/settlement/getDepartInfo.json',		//获取科室信息
			listAllDeparts:'mock/dataAnalyse/settlement/listAllOrgDeparts.json',
			getOrgDepartTotalCast:'mock/dataAnalyse/settlement/getOrgDepartTotalCast.json',
			getDepartDoctorsChartData:'mock/dataAnalyse/settlement/getDepartDoctorsChartData.json',
			getOrgTotalCostChartData:'mock/dataAnalyse/settlement/getOrgTotalCostChartData.json',
			queryDoctorByName:'mock/dataAnalyse/settlement/queryDoctorByName.json',
			listBaiscTableData:'indication/getOrgIndication.do',//'mock/dataAnalyse/settlement/listBaiscTableData.json',
			queryOrg:'mock/dataAnalyse/settlement/queryOrg.json',
			getOrgTableBasicData:'indication/getOrgTableBasicData.do',//'mock/dataAnalyse/settlement/getOrgTableBasicData.json', //getOrgTableBasicData
			getDeptTableBasicData:'indication/getDeptTableBasicData.do',//'mock/dataAnalyse/settlement/getDeptTableBasicData.json',
			queryOrgCount:'mock/dataAnalyse/settlement/queryOrgCount.json',
			getInsuranceCostInfo:'mock/dataAnalyse/settlement/getInsuranceCostInfo.json',
			getMedicalPeopleChartData:'mock/achievements/settlement/getMedicalPeopleChartData.json',//mys
            getAvgHospitalDayChartData:'mock/achievements/settlement/getAvgHospitalDayChartData.json',//获取平均住院日
            getOrgAvgCostChartData:'mock/achievements/settlement/getOrgAvgCostChartData.json',//mys
    		getAvgBedDayCostChartData:'mock/achievements/settlement/getAvgBedDayCostChartData.json'//mys
		},
		
		
		urls = {
				getIndicatorInfo:'indication/getIndicatorInfo.do',
				getAvgHospitalDayChartData:'kpi/getAvgHospitalDayChartData.do',
				getRegionOrgs:'indication/getRegionOrgs.do',
				getDepartments:'indication/getDepartments.do',
				getDoctors:'indication/getDoctors.do',
				listBaiscTableData:'indication/getOrgIndication.do',
				getOrgTableBasicData:'indication/getOrgTableBasicData.do',
				getDeptTableBasicData:'indication/getDeptTableBasicData.do',
				queryOrgCount:'indication/queryOrgCount.do'//'mock/dataAnalyse/settlement/queryOrgCount.json',
				
			};
	
	function getUrl(name) {
		if(debug) {
			return debugUrls[name];
		} else {
			return urls[name];
		}
	}
	
	var app = angular.module('fwa.services');
	app.service('SettleAnalyseService',['$http','BaseURL',function($http, baseUrl){
		
		var serv = this;
		serv.typeSelected = null;
		serv.checkedArray = [];
		serv.orgLevel = null;
		serv.medicalOrgInfoList;
		serv.medicalDeptInfoList;
		serv.loading = false;
		
		//指标类型
		serv.getIndicatorInfo = function(id) {
			var params = {
				tmp:Math.random(),
				id:id
			}
			return $http.get(baseUrl + getUrl('getIndicatorInfo'),{
				params:params
			});
		}
		
		//departments select data
		serv.listAllDeparts = function(id) {
			return $http.get(baseUrl + getUrl('listAllDeparts'),{
				params:{
					id:id,
					tmp:Math.random()
				}
			});
		}
		
		/**
		 * map:{key:value} 删除的条件
		 * 
		 */
		serv.removeFromCheckedArray = function(map) {
			var idx, data;
			idx = serv.indexOfCheckedArray(map);
			if(idx != -1) {
				data = serv.checkedArray[idx];
				serv.checkedArray.splice(idx,1);
				return data;
			}
			return null;
		}
		
		serv.checkedInit = function(arr) {
			$.each(serv.checkedArray, function(i){
				var target = _.findWhere(arr, {id:this.id});
				if(target) {
					target.checked = true;
					
					serv.checkedArray[i] = target;
				}
			});
		}
		
		serv.indexOfCheckedArray = function(map) {
			for(var i=0; i< serv.checkedArray.length;i++) {
				var data = serv.checkedArray[i];
				var b = true;
				$.each(map, function(k, v){
					if(v !== data[k]) {
						b = false;
					}
				});
				if(b) {
					return i;
				}
			}
			return -1;
		}
		
		/**
		 * 搜索医生
		 * 
		 * query:{
		 *     orgId:医院, 	
		 *     deptId:科室,
		 *     name:名称   
		 * }
		 * 
		 */
		serv.queryDoctor = function(query) {
			var params = query || {};
			params.tmp = Math.random();
			return $http.get(baseUrl + getUrl('queryDoctorByName'),{
				params:params
			});
		}
		
		/**
		 * 搜索机构
		 * 
		 * query:{
		 * 	level:等级,
		 * 	name:名称,
		 *  category:类型
		 * }
		 * 
		 */
		serv.queryOrg = function(query) {
			var params = query || {};
			params.tmp = Math.random();
			return $http.get(baseUrl + getUrl('queryOrg'),{
				params:params
			});
		}
		
		/**医疗机构**/
		this.initMedicalOrgInfoList = function(orgTypeList,orgLevelList){
			if(!serv.medicalOrgDatas){
				$http({
			    	 method: "get",
			    	 url: baseUrl+'community/getNewMedicalOrgInfoList.do',
			    	 params:{
			    		 orgTypeList:orgTypeList,
			    		 orgLevelList:orgLevelList
			    	 }
				}).success(function(response, status, headers, config){
					serv.medicalOrgInfoList = response.medicalOrgList;
				});
			}
		}
		
		/**医疗机构科室**/
		this.initMedicalDeptInfoList = function(paramsOrg,flag){
			//判断是根据机构id或者机构编码查询 flag：true  根据机构id flag：false  根据机构编码 
			var orgId = '',orgCode = '';
			if(flag){
				orgId = paramsOrg;
			}else{
				orgCode = paramsOrg;
			}
			if(!serv.medicalOrgDatas){
				$http({
			    	 method: "get",
			    	 url: baseUrl+'community/getMedicalDeptInfoList.do',
			    	 params:{
			    		 orgId:orgId,
			    		 orgCode:orgCode
			    	 }
				}).success(function(response, status, headers, config){
					serv.medicalDeptInfoList = response.medicalDeptList;
				});
			}
		}
		
		
	}]).
	
	//区域视图服务
	service('SettleAnalyseRegionService',['$http','BaseURL','SettleAnalyseService',
	                                      function($http, baseUrl,SettleAnalyseService){
		
		var serv = this;
			
		//机构数据列表
		serv.listRegionOrgs = function(params) {
			params = params || {};
			params.tmp = Math.random();
			params.dataType = SettleAnalyseService.typeSelected;
			return $http({
				url: baseUrl + getUrl('getRegionOrgs'),
				method:'POST',
				data:params
			});
		}
		
		//表基本数据,地区,级别
		serv.listBaiscTableData = function(data) {
			return $http.post(getUrl('listBaiscTableData'),{
				data:{
					tmp:Math.random(),
					indicator:data.indicator,
					dataType:SettleAnalyseService.typeSelected,
					date:data.date
				}
			});
		}
		
		serv.queryOrgCount = function(params) {
			params = params || {};
			params.tmp = Math.random();
			params.dataType = SettleAnalyseService.typeSelected;
			return $http({
				url: baseUrl + getUrl('queryOrgCount'),
				method:'POST',
				data:params
			});
		}
		
	}]).
	
	
	//机构视图服务
	service('SettleAnalyseOrgService',['$http','BaseURL','SettleAnalyseService',
	                                   function($http, baseUrl,SettleAnalyseService){
		
		var serv = this;
			
		//科室数据列表
		serv.listOrgDepartments = function(params) {
			params = params || {};
			params.tmp = Math.random();
			params.dataType = SettleAnalyseService.typeSelected;
			return $http({
				url: baseUrl + getUrl('getDepartments'),
				method:'GET',
				params:params
			});
		}
		
		serv.getOrgTableBasicData = function(indicator,orgId,date) {
			return $http({
				url: baseUrl + getUrl('getOrgTableBasicData'),
				method:'GET',
				params:{
					tmp: Math.random(),
					indicator:indicator,
					orgId:orgId,
					dataType: SettleAnalyseService.typeSelected,
					startDate:date.start,
					endDate:date.end
				}
			});
		}
		
		//获取当前医院名称
		serv.getOrgInfo = function(id) {
			return $http.get(baseUrl + getUrl('getOrgInfo'), {
				params:{
					tmp:Math.random(),
					id:id
				}
			});
		}
		
	}]).
	
	
	
	service('SettleAnalyseDepartService',['$http','BaseURL','SettleAnalyseService',
	                                      function($http, baseUrl,SettleAnalyseService){
		
		var serv = this;
		
		serv.deptId = null;
		
		serv.listDoctors = function(parmas) {
			parmas = parmas || {};
			parmas.tmp = Math.random();
			parmas.dataType = SettleAnalyseService.typeSelected;
			return $http({
				url: baseUrl + getUrl('getDoctors'),
				method:'GET',
				params:parmas
			});
		}
		
		serv.getDepartInfo = function(id) {
			return $http.get(baseUrl + getUrl('getDepartInfo'), {
				params:{
					tmp:Math.random(),
					id:id
				}
			});
		}
		
		serv.getDeptTableBasicData = function(indicator,id,date,orgCode) {
			return $http.get(baseUrl + getUrl('getDeptTableBasicData'), {
				params:{
					tmp:Math.random(),
					indicator:indicator,
					id:id,
					orgId:orgCode,
					dataType: SettleAnalyseService.typeSelected,
					startDate:date.start,
					endDate:date.end
				}
			});
		}
		
	}]).
	
	
	service('SettleAnalyseChartService', ['$http', 'BaseURL','SettleAnalyseService', 
	                                      function($http, baseUrl,SettleAnalyseService){
		
		var serv = this;
		
		//获取区域视图机构费用图数据
		serv.getOrgTotalCostChartData = function(orgIds) {
			return $http.post(baseUrl + getUrl('getOrgTotalCostChartData'),{
				tmp:Math.random(),
				view:SettleAnalyseService.view,
				orgIds:orgIds,
				dataType:SettleAnalyseService.typeSelected
			});
		}
		
		//mys
		serv.getMedicalPeopleChartData = function(orgIds) {
			return $http.post(baseUrl + getUrl('getMedicalPeopleChartData'),{
				orgIds:orgIds
			});
		}
		
		//mys
		serv.getAvgBedDayCostChartData = function(orgIds) {
			return $http.post(baseUrl + getUrl('getAvgBedDayCostChartData'),{
				orgIds:orgIds
			});
		}
		
		//获取指标数据
		serv.getKpiChartValue = function(arr,kpiId,date) {
			var ids = getDateIds(arr);
			return $http.post(baseUrl + '/kpi/getKpiChartValue.do',{
				ids:ids,
				kpiId:kpiId,
				dataType:SettleAnalyseService.typeSelected,
				startDate:date.start,
				endDate:date.end
			});
		}
		
		//获取chebox选中的值集合
		function  getDateIds(arr) {
			var ids = [];
			$.each(arr, function(){
				if(this.level == undefined && this.orgCategory == undefined){
					if (this.type=="region") {
						var json = {
									id: this.regionCode,
									type: this.type,
									name: this.name
								};
						ids.push(json);
					} 
					else if(this.type=="org") {
						var json = {
									id: this.orgCode,
									type: this.type,
									name: this.name,
									regionCode: this.regionCode
								};
						ids.push(json);
					} 
					else if(this.type=="depart") {
						var json = {
									id: this.deptCode,
									type: this.type,
									name: this.name,
									regionCode: this.regionCode,
									orgCode: this.orgCode
								};
						ids.push(json);
					} 
					else if( this.type == "doctor") {
						var json = {
									id: this.doctorCode,
									type: this.type,
									name: this.name,
									regionCode: this.regionCode,
									orgCode: this.orgCode,
									departCode: this.departCode
								};
						ids.push(json);
					}
				} 
				else {
					if (this.orgCategory && this.level <= 0) {
						var json = {
								id: this.orgCategory,
								type: 'orgCategory',
								name: this.name,
								regionCode: this.regionCode
							};
						ids.push(json);
					}
					else {
						var json = {
								id: this.level,
								type: 'level',
								name: this.name,
								regionCode: this.regionCode
							};
						ids.push(json);
					}
				}				
			});
			return ids;
		}
		
		//获取平均住院日图数据
		serv.getAvgHospitalDayChartData = function(orgIds,kpiKey,date) {
			
			return $http.post(baseUrl + '/kpi/getKpiValue.do',{
				orgIds:orgIds,
				kpiKey:kpiKey,
				date:date,
				view:SettleAnalyseService.view,
				dataType:SettleAnalyseService.typeSelected
			});
		}
		
	}]);
	
})();